package com.nbjtjc.safe.service.edu;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.EduEntryTrainingCompanyMapper;
import com.nbjtjc.safe.mapper.edu.EduEntryTrainingMapper;
import com.nbjtjc.safe.model.edu.EduEntryTraining;
import com.nbjtjc.safe.model.edu.EduEntryTrainingCompany;

@Service
public class EduEntryTrainingCompanyService {
	@Autowired
	EduEntryTrainingCompanyMapper eduEntryTrainingCompanyMapper;
	@Autowired
	EduEntryTrainingMapper eduEntryTrainingMapper;
	
	  public Result add(EduEntryTrainingCompany eduEntryTrainingCompany){
	        Result result=new Result();
	        if(ObjectUtil.isNull(eduEntryTrainingCompany)
	        		||ObjectUtil.isNull(eduEntryTrainingCompany.getTrainingId())
	        		){
	            result.setFalse("公司培训ID不能为空");
	            return result;
	        }

	        if(ObjectUtil.isNull(eduEntryTrainingCompany.getId())
	        		|| ObjectUtil.isNull(eduEntryTrainingCompanyMapper.selectByPrimaryKey(eduEntryTrainingCompany.getId()))){
	        	
	        	int res=eduEntryTrainingCompanyMapper.insertSelective(eduEntryTrainingCompany);
		            
		        if(res==0){
		            result.setFalse("插入失败");
		            return result;
	
		        }
		        }else
		        {
		        	int res=eduEntryTrainingCompanyMapper.updateByPrimaryKeySelective(eduEntryTrainingCompany);
		        	 if(res==0){
		        		 result.setFalse("更新失败");
				         return result;
	
		        	 }
		        }
	        result.setData(eduEntryTrainingCompany);
	        return result;
	    }



	public Result delete(int id){
	    Result result=new Result();
	    if(ObjectUtil.isNull(id)){
	        result.setFalse("删除对象不能为空");
	        return result;
	    }
	    int res=eduEntryTrainingCompanyMapper.deleteByPrimaryKey(id);
	    if(res==0){
	        result.setFalse("删除失败");
	        return result;
	        }
	    return result;
	}
	
	//根据条件查找调查项目
	
	public Result find(EduEntryTrainingCompany eduEntryTrainingCompany) {
	    Result result = new Result();
	    PageHelper.startPage(eduEntryTrainingCompany.getPageCurrent(), eduEntryTrainingCompany.getPageSize());

	    List<EduEntryTrainingCompany> eduEntryTrainingCompanys = eduEntryTrainingCompanyMapper.select(eduEntryTrainingCompany);
	    result.setData(new PageInfo<>(eduEntryTrainingCompanys));
	
	    return result;
	}

	
	public Result batchInput(EduEntryTrainingCompany eduEntryTrainingCompany){
	    Result result = new Result();
	    List<Integer> userIds = eduEntryTrainingCompany.getEduUserIds();
	    for(int userId:userIds){
	    	List<EduEntryTraining> eduEntryTrainings =eduEntryTrainingMapper.findIdByUserId(userId);
	    	if(CollectionUtil.isEmpty(eduEntryTrainings)){
		        result.setFalse("无此用户的三级培训记录");
		        return result;
	    	}
	    	if(eduEntryTrainings.size()>1){
		        result.setFalse("此用户有多个三级培训记录");
		        return result;
	    	}
	    	EduEntryTraining eduEntryTraining = eduEntryTrainings.get(0);
	    	eduEntryTrainingCompany.setTrainingId(eduEntryTraining.getId());
	    }
	    
	    return result;
	}
	
//	public Result find2(EduEntryTraining eduEntryTraining) {
//	    Result result = new Result();
//	    PageHelper.startPage(eduEntryTraining.getPageCurrent(), eduEntryTraining.getPageSize());
//
//	    List<EduEntryTraining> eduEntryTrainings = eduEntryTrainingCompanyMapper.search(eduEntryTraining);
//	    result.setData(new PageInfo<>(eduEntryTrainings));
//	
//	    return result;
//	}
}
